package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.ElecBicycleAdmin;
import com.mycompany.myapp.repository.ElecBicycleAdminRepository;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ElecBicycleAdmin.
 */
@RestController
@RequestMapping("/api")
public class ElecBicycleAdminResource {

    private final Logger log = LoggerFactory.getLogger(ElecBicycleAdminResource.class);

    private static final String ENTITY_NAME = "elecBicycleAdmin";

    private final ElecBicycleAdminRepository elecBicycleAdminRepository;

    public ElecBicycleAdminResource(ElecBicycleAdminRepository elecBicycleAdminRepository) {
        this.elecBicycleAdminRepository = elecBicycleAdminRepository;
    }

    /**
     * POST  /elec-bicycle-admins : Create a new elecBicycleAdmin.
     *
     * @param elecBicycleAdmin the elecBicycleAdmin to create
     * @return the ResponseEntity with status 201 (Created) and with body the new elecBicycleAdmin, or with status 400 (Bad Request) if the elecBicycleAdmin has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/elec-bicycle-admins")
    @Timed
    public ResponseEntity<ElecBicycleAdmin> createElecBicycleAdmin(@RequestBody ElecBicycleAdmin elecBicycleAdmin) throws URISyntaxException {
        log.debug("REST request to save ElecBicycleAdmin : {}", elecBicycleAdmin);
        if (elecBicycleAdmin.getId() != null) {
            throw new BadRequestAlertException("A new elecBicycleAdmin cannot already have an ID", ENTITY_NAME, "idexists");
        }
        ElecBicycleAdmin result = elecBicycleAdminRepository.save(elecBicycleAdmin);
        return ResponseEntity.created(new URI("/api/elec-bicycle-admins/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /elec-bicycle-admins : Updates an existing elecBicycleAdmin.
     *
     * @param elecBicycleAdmin the elecBicycleAdmin to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated elecBicycleAdmin,
     * or with status 400 (Bad Request) if the elecBicycleAdmin is not valid,
     * or with status 500 (Internal Server Error) if the elecBicycleAdmin couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/elec-bicycle-admins")
    @Timed
    public ResponseEntity<ElecBicycleAdmin> updateElecBicycleAdmin(@RequestBody ElecBicycleAdmin elecBicycleAdmin) throws URISyntaxException {
        log.debug("REST request to update ElecBicycleAdmin : {}", elecBicycleAdmin);
        if (elecBicycleAdmin.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        ElecBicycleAdmin result = elecBicycleAdminRepository.save(elecBicycleAdmin);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, elecBicycleAdmin.getId().toString()))
            .body(result);
    }

    /**
     * GET  /elec-bicycle-admins : get all the elecBicycleAdmins.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of elecBicycleAdmins in body
     */
    @GetMapping("/elec-bicycle-admins")
    @Timed
    public List<ElecBicycleAdmin> getAllElecBicycleAdmins() {
        log.debug("REST request to get all ElecBicycleAdmins");
        return elecBicycleAdminRepository.findAll();
    }

    /**
     * GET  /elec-bicycle-admins/:id : get the "id" elecBicycleAdmin.
     *
     * @param id the id of the elecBicycleAdmin to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the elecBicycleAdmin, or with status 404 (Not Found)
     */
    @GetMapping("/elec-bicycle-admins/{id}")
    @Timed
    public ResponseEntity<ElecBicycleAdmin> getElecBicycleAdmin(@PathVariable Long id) {
        log.debug("REST request to get ElecBicycleAdmin : {}", id);
        Optional<ElecBicycleAdmin> elecBicycleAdmin = elecBicycleAdminRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(elecBicycleAdmin);
    }

    /**
     * DELETE  /elec-bicycle-admins/:id : delete the "id" elecBicycleAdmin.
     *
     * @param id the id of the elecBicycleAdmin to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/elec-bicycle-admins/{id}")
    @Timed
    public ResponseEntity<Void> deleteElecBicycleAdmin(@PathVariable Long id) {
        log.debug("REST request to delete ElecBicycleAdmin : {}", id);

        elecBicycleAdminRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
